<template>
  <div class="terminal-info">
      <div class="container">
          <div class="info-title">终端管理</div>
          <div>
              <div class="filter-box">
                <!-- 商户名查询 -->
                <div class="filter-cell filter-by-shopname">
                    <span class="text-overflow">商户名：</span>
                    <el-input class="filter-input" clearable v-model="filters.shopname" placeholder="商户名查询"></el-input>
                </div>
                <!-- 商户号查询 -->
                <div class="filter-cell filter-by-shopid">
                    <span class="text-overflow">商户号：</span>
                    <el-input class="filter-input" clearable v-model="filters.shopId" placeholder="商户号查询"></el-input>
                </div>
                <!-- 终端号查询 -->
                <div class="filter-cell filter-by-terminal">
                    <span class="text-overflow">终端号：</span>
                    <el-input class="filter-input" clearable v-model="filters.terminalNo" placeholder="终端号查询"></el-input>
                </div>
              </div>
              <div class="filter-box">
                  <!-- 终端类型查询 -->
                <div class="filter-cell filter-by-terminal">
                    <span class="text-overflow">终端类型：</span>
                    <el-select v-model="filters.terminalType" clearable placeholder="--请选择--">
                      <el-option
                        v-for="(item,index) in options"
                        :key="index"
                        :label="item.label"
                        :value="item.value">
                      </el-option>
                    </el-select>
                </div>
                <div class="filter-by-startTime">
                    <span class="text-overflow">开始时间：</span>
                    <el-date-picker
                        v-model="filters.startTime"
                        type="date"
                        placeholder="选择日期">
                    </el-date-picker>
                </div>
                <div class="filter-by-endTime">
                    <span class="text-overflow">结束时间：</span>
                    <el-date-picker
                        v-model="filters.endTime"
                        type="date"
                        placeholder="选择日期">
                    </el-date-picker>
                </div>
              </div>
         

                <div class="filter-box">
                    <el-button style="width:200px" @click="queryTerminal">查询</el-button>
                </div>
        </div>
          <div v-loading="loading">
            <table border="0" cellspacing="0" v-if="terminalList.length !== 0">
              <thead>
                  <tr>
                      <th><div class="table-cell">终端号</div></th>
                      <th><div class="table-cell">二维码编号</div></th>
                      <th><div class="table-cell">所属门店</div></th>
                      <th><div class="table-cell">所属商户</div></th>
                      <th><div class="table-cell">终端类型</div></th>
                      <th><div class="table-cell">时间</div></th>
                      <th><div class="table-cell">是否开通</div></th>
                      <th><div class="table-cell">操作</div></th>
                  </tr>
              </thead>
              <tbody>
                <tr v-for="(cell,index) in terminalList" :key="index">
                  <td>
                      <div class="table-cell" v-html="cell.terminalNo"></div>
                  </td>
                  <td>
                      <div class="table-cell" v-html="cell.evmNo"></div>
                  </td>
                  <td>
                      <div class="table-cell" v-html="cell.storeName"></div>
                  </td>
                  <td>
                      <div class="table-cell" v-html="cell.shopName"></div>
                  </td>
                  <td>
                      <div class="table-cell" v-html="options[cell.terminalType].label"></div>
                  </td>
                  <td>
                      <div class="table-cell" v-html="timestampToTime(cell.createTime)"></div>
                  </td>
                  <td id="terminal-td">
                      <el-switch
                        v-model="cell.status"
                        @click.native="updateStatus(cell)"
                        active-text="打开"
                        inactive-text="关闭"
                        :active-value="0"
                        :inactive-value="1"
                        >
                      </el-switch>
                  </td>
                  <td>
                      <div class="table-cell">
                          <el-button @click="getDetail(cell)">查看</el-button>
                      </div>
                  </td>
                </tr>
              </tbody>
            </table>
            <div class="no-data" v-else>
                    暂无数据
            </div>
            <!--分页-->
            <div class="pages-control">
                  <el-pagination
                    @current-change="handleCurrentChange"
                    :current-page.sync="pages.currentPage"
                    :page-size="pages.page_size"
                    layout="total, prev, pager, next"
                    :total="pages.total">
                  </el-pagination>
            </div>
          </div>
      </div>
  </div>
</template>

<script>
export default {
  name:'terminal-info',
  data () {
    return {
      loading:false,
      status,
      terminalList:[],
      options: [{
        value: '0',
        label: '智能pos机'
      }, {
        value: '1',
        label: '台卡'
      }],
      filters:{
        shopId:'',//商户号
        shopName:"",//商户名
        terminalNo:'',//终端号
        terminalType:'',//终端类型
        startTime:"",//开始时间
        endTime:""//结束时间
      },
      pages:{
        total:100,
        page_size:20,
        currentPage:1
      }
    };
  },
  components: {},
  created() {
    this.queryTerminal();
  },
  mounted() {},
  methods: {
    timestampToTime(timestamp) {
        var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        let Y = date.getFullYear() + '-';
        let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        let D = date.getDate() + ' ';
        let h = date.getHours() + ':';
        let m = date.getMinutes() + ':';
        let s = date.getSeconds();
        return Y+M+D+h+m+s;
    },
    queryTerminal(){
        if(this.loading){
              this.$message("正在加载！请稍候！");
              return;
        }
        let url = `user/terminalInfo/find`;
        let params = `?page=${this.pages.currentPage-1}&size=${this.pages.page_size}`;
        ['shopId','shopName','terminalNo','terminalType','startTime','endTime'].forEach(element=>{
          if(this.filters[element]!== null && this.filters[element]!==''){
              params+=`&${element}=${this.filters[element]}`
          }
        });
        this.loading = true;
        this.$Http.get(url+params).then(res=>{
            this.loading = false;
            if(res.data.resp_code === "000000"){
              this.terminalList = res.data.result.content;
              this.pages.total = res.data.result.totalElements;
              if(res.data.result.length<=0){
                this.$notify.info({
                title: '消息',
                message: "暂无商户信息！"
                });
              }
            }else{
              this.$notify.info({
                  title: '消息',
                  message: res.data.resp_message
              }); 
            }
        }).catch(err=>{
            this.loading = false;
            this.$notify.error({
                title: '错误',
                message: '获取信息失败！'
            });
        }) 
    },
    /* 分页 */
    handleCurrentChange(value){
      this.queryTerminal();
    },
    /* 查看终端详情 */
    getDetail(cell){
      console.log("查看",cell);
        this.$router.push({
          name:"terminalManage",
          params:{
              shopId:cell.shopId
          }
      })
    },
    /* 更改终端开通状态 */
    updateStatus(cell){
          if(cell.terminalType==0){
            let submitNoopsyche = {...cell};
            let token = sessionStorage.getItem(1);
            let url = `user/terminal/update/pos/${token}`;
            delete submitNoopsyche.createTime;
            let params = ``;
            for(let key in submitNoopsyche){
                if(submitNoopsyche[key]!=='' && submitNoopsyche[key]!==null){
                    if(params===''){
                        params = key + '=' + submitNoopsyche[key];
                    }else{
                        params = params +'&'+ key + '=' + submitNoopsyche[key]
                    } 
                }      
            }
            params = encodeURI(params);
            this.$Http.post(url,params).then(res=>{
                if(res.data.resp_code === "000000"){
                    // 修改成功
                    this.$message({
                        showClose: true,
                        message: res.data.result,
                        type: 'success'
                    });
                }
            }).catch(err=>{
                this.$notify({
                    title: '修改失败！',
                    message: "修改失败！请重新尝试！",
                    type:"error"
                });
            })
          }
          if(cell.terminalType==1){
            let submitDecometerStatus = {...cell};
            let token = sessionStorage.getItem(1);
            let url = `user/terminal/update/tk/${token}`;
            delete submitDecometerStatus.createTime;
            let params = ``;
            for(let key in submitDecometerStatus){
                if(submitDecometerStatus[key]!=='' && submitDecometerStatus[key]!==null){
                    if(params===''){
                        params = key + '=' + submitDecometerStatus[key];
                    }else{
                        params = params +'&'+ key + '=' + submitDecometerStatus[key]
                    } 
                }      
            }
            params = encodeURI(params);
            this.$Http.post(url,params).then(res=>{
                if(res.data.resp_code === "000000"){
                    // 修改成功
                    this.$message({
                        showClose: true,
                        message: res.data.result,
                        type: 'success'
                    });
                }
            }).catch(err=>{
                this.$notify({
                    title: '修改失败！',
                    message: "修改失败！请重新尝试！",
                    type:"error"
                });
            })
          }
    }
  }
}

</script>
<style scoped>
.terminal-info{
    box-sizing: border-box;
    padding: 20px;
}
.container{
  border: 1px solid #ededed;
}
.info-title{
    height: 50px;
  line-height: 50px;
  font-size: 26px;
  color: #409eff;
  padding: 5px 20px;
  border-bottom: 1px solid #ededed;
}
.filter-box{
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 15px;
  flex-wrap: nowrap;
  overflow: hidden;
}
.filter-by-endTime{
  display: flex;
  justify-content: space-around;
  align-items: center;
  flex-wrap: nowrap;
}
.filter-cell{
  display: flex;
  justify-content: space-around;
  align-items: left;
  flex-wrap: nowrap;
}
.text-overflow{
    display: inline-block;
    min-width: 80px;
}
table{
    background-color: #FFF;
    width: 100%;
    font-size: 12px;
    min-width: 900px;
}
th{
    position: relative;
    word-wrap: normal;
    text-overflow: ellipsis;
    vertical-align: middle;
    border-bottom: 1px solid #ebeef5;
    
}
thead{
    padding: 8px 0;
    color: #909399;
}
tr{
    padding: 8px 0;
}
tr:hover{
    background-color: #F5F7FA;
}
td{
    border: none;
    border-bottom: 1px solid #ebeef5;
}
.table-cell{
    white-space: normal;
    word-break: break-all;
    line-height: 22px;
    padding: 10px 10px;
    text-align: center;
    min-width: 40px;
    box-sizing: border-box;
}
.no-data{
    text-align: center;
    padding: 20px ;
    border: 1px solid #dbdbdb;
    color: #dbdbdb;
}
.pages-control{
    display: flex;
    justify-content: center;
    align-items: center;
    height: 40px;
}
#terminal-td{
  width: 116px;
}
</style>